package com.wbcs.system.component.application.report;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;

import com.jf.render.TemplateRender;
import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.config.component.application.report.ColBean;
import com.wbcs.config.component.application.report.ConditionBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.ReportAssistant;
import com.wbcs.system.buttons.AbsButtonType;
import com.wbcs.system.component.application.report.abstractreport.AbsListReportType;
import com.wbcs.system.component.application.report.abstractreport.AbsReportType;
import com.wbcs.system.component.container.AbsContainerType;
import com.wbcs.util.Consts;
import com.wbcs.util.Consts_Private;

public class JfHtmlReportType extends AbsListReportType{
	
	private AbsContainerType parentContainerType;
	private IComponentConfigBean reportBean;
	private ReportRequest rrquest;
	
	public JfHtmlReportType(AbsContainerType parentContainerType, IComponentConfigBean comCfgBean,
			ReportRequest rrequest) {
		super(parentContainerType, comCfgBean, rrequest);
		this.parentContainerType = parentContainerType;
		this.reportBean = comCfgBean;
		this.rrequest = rrequest;
	}

	@Override
	public String showReportData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getColSelectedMetadata() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void showReportOnPlainExcel(Workbook workbook) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void showReportOnPdfWithoutTpl() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int getTotalColCount() {
		List<ColBean> lstColBeans=rbean.getDbean().getLstCols();
        int cnt=0;
        for(ColBean cbean:lstColBeans)
        {
            if(cacheDataBean.getColDisplayModeAfterAuthorize(cbean)<=0) continue;
            if(cbean.isControlCol()) continue;
            cnt++;
        }
        return cnt;
	}

	@Override
	protected String getDefaultNavigateKey() {
		// TODO Auto-generated method stub
		return Consts.LISTREPORT_NAVIGATE_DEFAULT;
	}

	@Override
	public String showReportData(boolean showtype) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 模板初始化
	 * @param rbTmp 
	 * @param dynTplPath 
	 */
	public void initJfHtml(final ReportBean rbTmp, String dynTplPath){
		
		if(rrequest.getRequest().getAttribute(Consts_Private.REPORT_TEMPLATE) != null)return;
		AbsReportType reportTypeObj = rrequest.getDisplayReportTypeObj(rbTmp.getId());
        List<Object> dataList = reportTypeObj.getLstReportData();
        List<ColBean> colList = rbTmp.getDbean().getLstCols();
        //数据模板化
        Map<String,Map<String,Object>> rMap = null;
        if(rrequest.getRequest().getAttribute(rbTmp.getPageBean().getId()) != null){
        	rMap = (Map<String, Map<String, Object>>) rrequest.getRequest().getAttribute(rbTmp.getPageBean().getId());
        }else{
        	rMap = new HashMap<String,Map<String,Object>>();
        }
        rMap.put(rbTmp.getId(),new HashMap<String,Object>(){{
    		//承载数据
    		put(Consts_Private.TAGNAME_DATA, new ArrayList<Map<String,Object>>());
    		//承载按钮
    		put(Consts_Private.TAGNAME_BUTTON, new HashMap<String,Object>());
    		//承载条件
    		put(Consts_Private.TAGNAME_CONDITION, new HashMap<String,Object>());
    	}});
        
        //装载数据
        if(dataList != null && dataList.size() > 0){
        	for(int i=0;i<dataList.size();i++){
            	Object rowDataObj = dataList.get(i);
            	Map<String,Object> tempMap = new HashMap<String,Object>();
            	for (ColBean colBean : colList) {
            		tempMap.put(colBean.getColumn(), ReportAssistant.getInstance().getPropertyValue(rowDataObj,colBean.getColumn()));
    			}
            	((List<Map<String,Object>>)rMap.get(rbTmp.getId()).get(Consts_Private.TAGNAME_DATA)).add(tempMap);
            }
        }
        //装载按钮
        if(rbTmp.getButtonsBean() != null){
        	for(AbsButtonType buttonType : rbTmp.getButtonsBean().getAllDistinctButtonsList()){
        		((Map<String,Object>)rMap.get(rbTmp.getId()).get(Consts_Private.TAGNAME_BUTTON)).put(buttonType.getName(), rbTmp.getPageBean().getId()+"."+rbTmp.getId()+"."+buttonType.getName());
            }
        }
        //装载条件
        if(rbTmp.getSbean().getLstConditions().size() > 0){
        	for(ConditionBean cBean : rbTmp.getSbean().getLstConditions()){
        		((Map<String,Object>)rMap.get(rbTmp.getId()).get(Consts_Private.TAGNAME_CONDITION)).put(cBean.getName(), rbTmp.getPageBean().getId()+"."+rbTmp.getId()+"."+cBean.getName());
            }
        }
        rrequest.getRequest().setAttribute(rbTmp.getPageBean().getId(), rMap);
        final Map<String, Map<String, Object>> rrMap = rMap;
		try {
			if(dynTplPath != null && !"".equals(dynTplPath.trim())){
				TemplateRender.engine
				.getTemplate(dynTplPath,rrequest.getRequest(),rrequest.getWResponse().getResponse())
				.render(new HashMap<String,Object>(){{put(rbTmp.getPageBean().getId(), rrMap);}},rrequest.getWResponse().getResponse().getWriter(),rrequest.getRequest(),rrequest.getWResponse().getResponse());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
